var express = require('express')
  , app = express();


var querystring = require('querystring');
var util = require('util');
//和数据库建立连接
var mysql  = require('mysql');  
var connection = mysql.createConnection({     
  host     : 'localhost',       
  user     : 'root',              
  password : '123456',       
  port: '3306',                   
  database: 'todo', 
}); 
 
connection.connect(); 

//解决跨域
app.all('*',function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With');
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  if (req.method == 'OPTIONS') {
    res.send(200);
  }
  else {
    next();
  }
});

// 查询列表
app.get('/todo/list', function (req, res) {
  let [addSql,addSqlParams] = ['',[]]
  addSql = 'SELECT * FROM list ORDER BY weight DESC, create_time DESC';
  connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
      console.log('[ERROR] - ',err.message);
      return;
    } 
    if(result.length){
      let params = {
        code:1,
        message:"查询成功",
        data:result
      }     
      res.send(params)
    }else{
      let params = {
        code:0,
        message:"暂无数据"
      }     
      res.send(params)
    }     
  });
});
// 修改todo的名称
app.put('/todo', function (req, res) {
  let [addSql,addSqlParams] = ['',[]]
  addSql = 'UPDATE list SET title = ? WHERE id = ?';
  // console.log(req.query)
  addSqlParams = [req.query.title, req.query.id]
  connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
      console.log('[ERROR] - ',err.message);
      return;
    } 
    if(result.affectedRows > 0){
      let params = {
        code:1,
        message:"修改成功"
      }     
      res.send(params)
    }else{
      let params = {
        code:0,
        message:"修改失败"
      }     
      res.send(params)
    }     
  });
});
// 删除todo
app.delete('/todo', function (req, res) {
  let [addSql,addSqlParams] = ['',[]]
  addSql = 'DELETE FROM list WHERE id = ?';
  // console.log(req.query)
  addSqlParams = [req.query.id]
  connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
      console.log('[ERROR] - ',err.message);
      return;
    } 
    if(result.affectedRows > 0){
      let params = {
        code:1,
        message:"删除成功"
      }     
      res.send(params)
    }else{
      let params = {
        code:0,
        message:"删除失败"
      }     
      res.send(params)
    }     
  });
});
// 添加todo
app.post('/todo', function (req, res) {
  let [addSql,addSqlParams] = ['',[]]
  addSql = 'INSERT INTO list(title) VALUES(?)';
  // console.log(req.query)
  addSqlParams = [req.query.title]
  connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
      console.log('[ERROR] - ',err.message);
      return;
    } 
    if(result.affectedRows > 0){
      let params = {
        code:1,
        message:"添加成功"
      }     
      res.send(params)
    }else{
      let params = {
        code:0,
        message:"添加失败"
      }     
      res.send(params)
    }     
  });
});
// 取消收藏
app.put('/todo/unstar', function (req, res) {
  let [addSql,addSqlParams] = ['',[]]
  addSql = 'UPDATE list SET weight = weight - 1 WHERE id = ?';
  // console.log(req.query)
  addSqlParams = [req.query.id]
  connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
      console.log('[ERROR] - ',err.message);
      return;
    } 
    if(result.affectedRows > 0){
      let params = {
        code:1,
        message:"取消收藏成功"
      }     
      res.send(params)
    }else{
      let params = {
        code:0,
        message:"取消收藏失败"
      }     
      res.send(params)
    }     
  });
});
// 收藏
app.put('/todo/star', function (req, res) {
  let [addSql,addSqlParams] = ['',[]]
  addSql = 'UPDATE list SET weight = weight + 1 WHERE id = ?';
  // console.log(req.query)
  addSqlParams = [req.query.id]
  connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
      console.log('[ERROR] - ',err.message);
      return;
    } 
    if(result.affectedRows > 0){
      let params = {
        code:1,
        message:"收藏成功"
      }     
      res.send(params)
    }else{
      let params = {
        code:0,
        message:"收藏失败"
      }     
      res.send(params)
    }     
  });
});
// 搜索
app.get('/todo/search', function (req, res) {
  let [addSql,addSqlParams] = ['',[]]
  addSql = "select * from list where title like ?";
  addSqlParams = ['%'+req.query.keyword+'%']
  connection.query(addSql,addSqlParams,function (err, result) {
    if(err){
      console.log('[ERROR] - ',err.message);
      return;
    } 
    if(result.length){
      let params = {
        code:1,
        message:"查询成功",
        data: result
      }     
      res.send(params)
    }else{
      let params = {
        code:0,
        message:"暂无数据"
      }     
      res.send(params)
    }     
  });
});
var server = app.listen(8081, function () {
  var port = server.address().port
  console.log("应用实例，访问地址为 http://localhost:%s", port)
})